package pers.zh.parking.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import pers.zh.parking.R;
import pers.zh.parking.common.Const;
import pers.zh.parking.data.entity.ParkingLot;
import pers.zh.parking.di.component.DaggerLotComponent;
import pers.zh.parking.mvp.contract.LotContract;
import pers.zh.parking.mvp.presenter.LotPresenter;
import pers.zh.parking.mvp.ui.adapter.LotManageAdapter;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class LotManageActivity extends BaseActivity<LotPresenter> implements LotContract.View {

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.toolbar_left)
    RelativeLayout toolbarLeft;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.rl)
    SmartRefreshLayout rl;

    private LotManageAdapter mAdapter;

    private int page = 1;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLotComponent //如找不到该类,请编译一下项目
                           .builder().appComponent(appComponent).view(this).build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_reservation; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ivLeft.setOnClickListener(v -> killMyself());
        tvTitle.setText("停车场管理");
        initList();
        rl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getLots(page);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                mPresenter.getLots(page);
            }
        }, recyclerView);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ParkingLot item = mAdapter.getData().get(position);
                if (item == null) {
                    return;
                }
                Intent intent = new Intent(LotManageActivity.this, SpaceActivity.class);
                intent.putExtra(Const.Keys.KEY_PARKING_LOT, item);
                launchActivity(intent);
            }
        });
        mPresenter.getLots(page);
    }

    private void initList() {
        mAdapter = new LotManageAdapter(new ArrayList<>());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void showLoading() {
//        rl.autoRefresh();
    }

    @Override
    public void hideLoading() {
        rl.finishRefresh();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void replaceData(List<ParkingLot> list) {
        mAdapter.replaceData(list);
    }

    @Override
    public void addData(List<ParkingLot> list) {
        mAdapter.addData(list);
        mAdapter.loadMoreComplete();
    }

    @Override
    public void loadMoreEnd() {
        mAdapter.loadMoreEnd();
    }
}
